/*
 * ErrorDialog.java
 *
 * Created on September 8, 2005, 10:55 AM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.ui.component.BJJButton;
import com.bff.slimserver.musicjukebox.ui.settings.PropertiesFrame;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.Dimension;

/**
 *
 * @author  Bill
 */
public class ErrorDialog extends MusicJDialog {

    private String errorMessage;
    private String message;
    private Frame frame;
    private boolean showSettings;
    private boolean endApplication;

    /** Creates new form ErrorDialog */
    public ErrorDialog(Frame frame, String message, Exception exception) {
        this(frame, message, exception.getMessage(), true, true);
    }

    public ErrorDialog(Frame frame, String message, Exception exception, boolean showSettingsScreen, boolean endApplication) {
        this(frame, message, exception.getMessage(), showSettingsScreen, endApplication);
    }

    public ErrorDialog(Frame frame, String message, String errorMsg, boolean showSettingsScreen) {
        this(frame, message, errorMsg, true, true);
    }

    public ErrorDialog(Frame frame, String message, String errorMsg, boolean showSettingsScreen, boolean endApplication) {
        super(frame);
        this.errorMessage = errorMsg;
        this.message = message;
        this.frame = frame;
        this.showSettings = showSettingsScreen;
        this.endApplication = endApplication;

        initComponents();

        if (errorMsg == null) {
            remove(textAreaException);
            remove(jPanel1);
            add(jPanel1, BorderLayout.CENTER);
        }

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        int heightScreen = d.height / 2;
        int widthScreen = d.width / 2;
        if (errorMsg == null) {
            setSize(new Dimension(400, 150));
        } else {
            setSize(new Dimension(400, 300));
        }
        setLocation(widthScreen - (int) (getWidth() / 2), heightScreen - (int) (getHeight() / 2));
        setVisible(true);
    }

    /*
    ErrorDialog(Frame frame, String message) {
    this(frame, message, null);
    }
     */
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnPanel = new BJJPanel();
        btnOK = new BJJButton();
        btnProps = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        textAreaException = new javax.swing.JTextArea(errorMessage);
        jPanel1 = new BJJPanel();
        lblError = new javax.swing.JLabel(message);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Error");

        btnPanel.setLayout(new java.awt.GridBagLayout());

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        btnPanel.add(btnOK, gridBagConstraints);

        btnProps.setText("Remove Props");
        btnProps.setToolTipText("Remove the props file and reenter parameters");
        btnProps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPropsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnProps, gridBagConstraints);

        jLabel1.setText("Click remove props to reenter SqueezeCenter settings");
        btnPanel.add(jLabel1, new java.awt.GridBagConstraints());

        getContentPane().add(btnPanel, java.awt.BorderLayout.SOUTH);

        textAreaException.setEditable(false);
        textAreaException.setLineWrap(true);
        textAreaException.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 20, 20, 20));
        getContentPane().add(textAreaException, java.awt.BorderLayout.CENTER);

        lblError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblError.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        lblError.setFocusable(false);
        lblError.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jPanel1.add(lblError);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        this.dispose();
        if (isEndApplication()) { frame.dispose(); }
        if (showSettings) {
            new PropertiesFrame(true).setVisible(true);
        } else {
            if (isEndApplication()) {
                System.exit(0);
            }
        }
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnPropsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPropsActionPerformed
        JukeboxProperties.getInstance().removeProperties();
        btnOK.doClick();
    }//GEN-LAST:event_btnPropsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JButton btnProps;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblError;
    private javax.swing.JTextArea textAreaException;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the endApplication
     */
    public boolean isEndApplication() {
        return endApplication;
    }

    /**
     * @param endApplication the endApplication to set
     */
    public void setEndApplication(boolean endApplication) {
        this.endApplication = endApplication;
    }
}
